def switch_to_persp():
    target_cam = "persp"
    
    # Verify the default persp camera exists
    if not cmds.objExists(target_cam):
        cmds.warning("The default 'persp' camera could not be found.")
        return

    # Identify the active panel
    active_panel = cmds.getPanel(withFocus=True)
    
    # If the focus isn't a viewport, find the first visible model panel
    if cmds.getPanel(typeOf=active_panel) != 'modelPanel':
        visible_panels = cmds.getPanel(visiblePanels=True)
        active_panel = next((p for p in visible_panels if cmds.getPanel(typeOf=p) == 'modelPanel'), None)

    if active_panel:
        cmds.modelEditor(active_panel, edit=True, camera=target_cam)
        print(f"View switched back to {target_cam}")
    else:
        cmds.warning("No active 3D viewport found to switch.")

switch_to_persp()
